package br.com.sis.rh.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import br.com.sis.rh.entity.AreaAtuacao;
import br.com.sis.rh.entity.Candidato;
import br.com.sis.rh.entity.Competencia;
import br.com.sis.rh.entity.Segmento;
import br.com.sis.rh.facade.AreaAtuacaoFacade;
import br.com.sis.rh.facade.CandidatoFacade;
import br.com.sis.rh.facade.CompetenciaFacade;
import br.com.sis.rh.facade.SegmentoFacade;

@Controller
@Scope("request")
public class ComumAuxAction {

	@Autowired
	private CandidatoFacade candidatoFacade;
	
    @Autowired
    private EhCacheCacheManager ehCache;
	
	@Autowired
	private AreaAtuacaoFacade areaAtuacaoFacade;	
	
	@Autowired
	private SegmentoFacade segmentoFacade;
	
	@Autowired
	private CompetenciaFacade competenciaFacade;
	
	private List<AreaAtuacao> comboAreaAtuacao;
	
	private List<Segmento> comboSegmento;
	
	private List<Competencia> comboCompetencia;
	
	@SuppressWarnings("unchecked")
	public void carregaComboAreaAtuacao() {
		comboAreaAtuacao = new ArrayList<>();
		
		if(ehCache.getCache("areaAtuacao").get("1") != null) {
			comboAreaAtuacao = (ArrayList<AreaAtuacao>) ehCache.getCache("areaAtuacao").get("1").get();
		} else {
			comboAreaAtuacao = areaAtuacaoFacade.listar(new AreaAtuacao());
			ehCache.getCache("areaAtuacao").put("1", comboAreaAtuacao);
		}
	}

	@SuppressWarnings("unchecked")
	public void carregaComboSegmento() {
		comboSegmento = new ArrayList<>();
		
		if(ehCache.getCache("segmento").get("1") != null) {
			comboSegmento = (ArrayList<Segmento>) ehCache.getCache("segmento").get("1").get();
		} else {
			comboSegmento = segmentoFacade.listar(new Segmento());
			ehCache.getCache("segmento").put("1", comboSegmento);
		}			
	}
	
	@SuppressWarnings("unchecked")
	public void carregaComboCompetencia() {
		comboCompetencia = new ArrayList<>();
		
		if(ehCache.getCache("competencia").get("1") != null) {
			comboCompetencia = (ArrayList<Competencia>) ehCache.getCache("competencia").get("1").get();
		} else {
			comboCompetencia = competenciaFacade.listar(new Competencia());
			ehCache.getCache("competencia").put("1", comboCompetencia);
		}			
	}
	
	public Candidato carregarListaPorCandidato(Candidato candidato, String joinTable) {
		return candidatoFacade.listarPorObjeto(candidato, joinTable);
	}
	
	public Double calculaPorcentagemPreenchida(Integer numeroTotalAtributos, Integer numeroAtributosPreenchidos) {
		Double porcentagem =  Double.valueOf((100*numeroAtributosPreenchidos)/numeroTotalAtributos);
		
		return porcentagem;
	}

	public List<AreaAtuacao> getComboAreaAtuacao() {
		return comboAreaAtuacao;
	}

	public void setComboAreaAtuacao(List<AreaAtuacao> comboAreaAtuacao) {
		this.comboAreaAtuacao = comboAreaAtuacao;
	}

	public List<Segmento> getComboSegmento() {
		return comboSegmento;
	}

	public void setComboSegmento(List<Segmento> comboSegmento) {
		this.comboSegmento = comboSegmento;
	}

	public List<Competencia> getComboCompetencia() {
		return comboCompetencia;
	}

	public void setComboCompetencia(List<Competencia> comboCompetencia) {
		this.comboCompetencia = comboCompetencia;
	}
}
